﻿using System;
using System.Collections.Generic;
using System.Data;
using ContractManager.Entity;
using Speed.Data;

namespace ContractManager.BLL.Business
{
    public static class ClsEmployees
    {
        public static List<Employees> SelectAll()
        {
            using (var db = Sys.NewDb())
            {
                return Data.Base.Employees.Select(db);
            }
            
        }

        public static void Insert(Employees capital)
        {
            using (var db = Sys.NewDb())
            {
                db.Insert<Employees>(capital, EnumSaveMode.Requery);
            }
            
        }

        public static void Update(Employees capital)
        {
            using (var db = Sys.NewDb())
            {
                db.Update<Employees>(capital);
            }
            
        }

        public static void ChangePass(string userName, string passOld, string passNew)
        {
            using (var db = Sys.NewDb())
            {
                string commandText =
                String.Format("UPDATE Employees SET Password='{0}' WHERE UserName='{1}' AND Password='{2}'", passNew,
                              userName, passOld);
                db.ExecuteNonQuery(commandText, CommandType.Text);
            }
            
        }

        public static Employees SelectByPk(Int32 empId)
        {
            using (var db = Sys.NewDb())
            {
                return db.SelectSingle<Employees>(string.Format("ID={0}", empId));
            }
            
        }

        public static Employees SelectByUserName(string userName)
        {
            using (var db = Sys.NewDb())
            {
                return db.SelectSingle<Employees>(String.Format("UserName='{0}'", userName));
                //return Data.Base.Employees.Select(db, String.Format("UserName='{0}'", userName));
            }
            
        }

        public static int DeleteByPk(Int32 empId)
        {
            using (var db = Sys.NewDb())
            {
                return db.Delete<Employees>(string.Format("ID={0}", empId));
            }
            
        }
    }
}
